// LightTable.cpp: implementation of the CLightTable class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "MVI2002inc.h"
#include "../Library/ComDevice/Light.h"
#include "../Library/Utility/FileSupport.h"
#include "../Library/Utility/Profile.h"
#include "LightTable.h"

#include "Config.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLightTable::CLightTable()
{
}

CLightTable::~CLightTable()
{

}

void CLightTable::Clear()
{
	m_horzLightMap.clear();
	m_vertLightMap.clear();
	m_vert2LightMap.clear();
}

BOOL CLightTable::Load()
{
	CString fileName;	
	fileName.Format("%s\\%s", GetConfig()->GetSetupPath(), LIGHT_TABLE_FILE);
	if (!CFileSupport::IsFile(fileName)) {
		return FALSE;
	}

	Clear();
	
	CProfile profile;
	profile.SetProfileFile(fileName);

	long value;
	CString keyName;
	
	for (int i=0; i<256; i++) {
		keyName.Format("Light Level %4d", i);
		value = profile.GetParameter("Horz Light Value", keyName, i);

		m_horzLightMap[i] = value;
	}

	for (i=0; i<256; i++) {
		keyName.Format("Light Level %4d", i);
		value = profile.GetParameter("Vert Light Value", keyName, i);

		m_vertLightMap[i] = value;
	}

	if ( GetConfig()->IsCoaxialInstalled() ) {
		for ( i=0; i<256; i++) {
			keyName.Format("Light Level %4d", i);
			value = profile.GetParameter("Vert2 Light Value", keyName, i);

			m_vert2LightMap[i] = value;
		}
	}

	return TRUE;
}

long CLightTable::DevToLog(long type, int devValue)
{
	switch(type) {
	case HorzLight:
		return m_horzLightMap[devValue];
	case VertLight:
		return m_vertLightMap[devValue];
	default:
	case Vert2Light:
		{
			if ( GetConfig()->IsCoaxialInstalled() ) {
				return m_vert2LightMap[devValue];
			}
		}	
		return devValue;
	}
}

void CLightTable::DevToLog(LightInfo* lightInfo)
{
	lightInfo->SetHorizontal(DevToLog(HorzLight, lightInfo->GetHorizontal()));
	lightInfo->SetVertical(DevToLog(VertLight, lightInfo->GetVertical()));
	lightInfo->SetVerticalII(DevToLog(Vert2Light, lightInfo->GetVerticalII()));
}

void CLightTable::DevToLog(long type, LightInfo* lightInfo)
{

}

void CLightTable::LogToDev(LightInfo* lightInfo)
{
	lightInfo->SetHorizontal( LogToDev(HorzLight, lightInfo->GetHorizontal()));
	lightInfo->SetVertical( LogToDev(VertLight, lightInfo->GetVertical()));
	lightInfo->SetVerticalII(LogToDev(Vert2Light, lightInfo->GetVerticalII()));
}

void CLightTable::TransDevToLog(LightInfo* lightInfo)
{
	lightInfo->SetHorizontal((BYTE)(DevToLog(HorzLight, lightInfo->GetHorizontal())));
	lightInfo->SetVertical((BYTE)(DevToLog(VertLight, lightInfo->GetVertical())));
	lightInfo->SetVerticalII((BYTE)(DevToLog(Vert2Light, lightInfo->GetVerticalII())));
}

void CLightTable::TransLogToDev(LightInfo* lightInfo)
{
	lightInfo->SetHorizontal((BYTE)(LogToDev(HorzLight, lightInfo->GetHorizontal())));
	lightInfo->SetVertical((BYTE)(LogToDev(VertLight, lightInfo->GetVertical())));
	lightInfo->SetVerticalII((BYTE)(LogToDev(Vert2Light, lightInfo->GetVerticalII())));
}

long CLightTable::LogToDev(long type, int logValue)
{
	int preValue;
	map<int, int>::iterator itr, end;
	switch(type) {
	default:
		ASSERT(0);
	case HorzLight:
		itr = m_horzLightMap.begin();
		end = m_horzLightMap.end();
		break;
	case VertLight:
		itr = m_vertLightMap.begin();
		end = m_vertLightMap.end();
		break;
	case Vert2Light:
		if ( !GetConfig()->IsCoaxialInstalled() ) {
			return 0;
		}
		itr = m_vert2LightMap.begin();
		end = m_vert2LightMap.end();
		break;
	}

	if ((*itr).second >= logValue)
		return (*itr).first;
	preValue = (*itr).second;
	itr++;
	for (; itr != end; itr++) {
		if (preValue < logValue && (*itr).second >= logValue)
			return (*itr).first;
		preValue = (*itr).second;
	}
	itr--;
	return (*itr).first;
//	return preValue;
}

void CLightTable::SetLightValue( long Type, int devValue, int logValue)
{	
	switch ( Type )
	{
	case HorzLight:
		m_horzLightMap[devValue] = logValue;
		break;

	case VertLight:
		m_vertLightMap[devValue] = logValue;
		break;

	case Vert2Light:
		m_vert2LightMap[devValue] = logValue;
		break;
	}
}


void CLightTable::Save()
{

	CString fileName;	
	fileName.Format("%s\\%s", GetConfig()->GetSetupPath(), LIGHT_TABLE_FILE);
	
	CString strKey, strValue;
	CProfile profile;
	profile.SetProfileFile(fileName);
		
	for ( int level = 0; level < 256; level++) {		
		strKey.Format("Light Level %4d", level);
		profile.SetParameter( "Horz Light Value", strKey, m_horzLightMap[level]);
	}
	
	for ( level = 0; level < 256; level++) {
		strKey.Format("Light Level %4d", level);
		profile.SetParameter( "Vert Light Value", strKey, m_vertLightMap[level]);
	}

	if ( GetConfig()->IsCoaxialInstalled() ) {
		for ( level = 0; level < 256; level++) {
			strKey.Format("Light Level %4d", level );
			profile.SetParameter( "Vert2 Light Value", strKey, m_vert2LightMap[level]);
		}
	}
}